package sdufe.lele.controller;


import org.apache.shiro.util.ThreadContext;
import org.springframework.web.bind.annotation.*;
import sdufe.lele.annotation.authToken;
import sdufe.lele.dao.BrokerMapper;
import sdufe.lele.dao.FinancialMapper;
import sdufe.lele.dao.UserinfoMapper;
import sdufe.lele.model.Broker;
import sdufe.lele.model.Financial;
import sdufe.lele.model.Financial2user;
import sdufe.lele.model.Userinfo;
import sdufe.lele.result.ApiResult;
import sdufe.lele.result.ListResult;
import sdufe.lele.result.MapResult;
import sdufe.lele.result.Result;
import sdufe.lele.service.BalanceService;


import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/financial")
public class FinancialController {


    @Resource
    private FinancialMapper financialMapper;

    @Resource
    private BrokerMapper brokerMapper;

    @Resource
    private UserinfoMapper userinfoMapper;

    @Resource
    private BalanceService balanceService;

    @GetMapping("/get_product.do")
    @authToken
    public Result get_product(@RequestParam(name = "page") Integer page) {

        ListResult listResult = new ListResult();
        listResult.setAll(financialMapper.selectByPage(page - 1, page + 100));
        return listResult;
    }

    @PostMapping("/add_financial.do")
    @authToken
    public Result insert_financial(@RequestParam(name = "pid") Integer fid,
                                   @RequestParam(name = "balance") BigDecimal balance,
                                   @RequestParam(name = "broker") String broker ,
                                   @RequestParam(name = "description")String desc,
                                   @RequestParam(name = "name")String name) {
        String phone = (String)ThreadContext.get("user-phone");
        Financial2user financial2user = new Financial2user();
        financial2user.setBalance(balance);
        financial2user.setUserphone(phone);
        financial2user.setBroker(broker);
        financial2user.setDescription(desc);
        financial2user.setCurrentbalance(balance);

        financial2user.setFuid(fid);

        Financial financial= financialMapper.selectFinancialById(fid);
        financial2user.setFinancialname(financial.getFinancialname());

        Date date = new Date(System.currentTimeMillis());

        financial2user.setCreatetime(date);

        financial2user.setCycle(financial.getFinancialcycle());

        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.add(Calendar.MONTH,financial2user.getCycle());

        financial2user.setEndtime(cal.getTime());

        balanceService.updateBalance(financial2user);
        return new ApiResult();
    }

    @GetMapping("/fin_init.do")
    @authToken
    public Result init() {
        MapResult mapResult= new MapResult();
        String phone = (String) ThreadContext.get("user-phone");
        List<Broker> brokerList = brokerMapper.selectAllByPhone(phone);
        Integer page = 0;

        List<Financial> financialList = financialMapper.selectByPage(page , page + 100);
        mapResult.put("type", financialList);
        mapResult.put("broker", brokerList);
        return mapResult;
    }

    @GetMapping("/record.do")
    @authToken
    public Result record(@RequestParam(name = "page") Integer page){
        ListResult listResult  = new ListResult();
        String phone = (String) ThreadContext.get("user-phone");
        listResult.setAll(financialMapper.selectAllByPhone(phone,page-1,page+100));
        return listResult;
    }
}
